package 富婆梦.第一阶段.第八周Scoket.作业1;

import sun.print.PSPrinterJob;

import java.io.*;
import java.util.Properties;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第八周Scoket.作业1
 * @ClassName: DeviceManage2
 * @Author: hemengjin
 * @Description: 操作device对象的类，提供存储device对象、读取device对象的两个方法,
 * *            使用非Properties格式文件进行存储，想办法使保存的数据尺寸最小化（提示：使用纯二进制存储）
 * @Date:2021/9/25 21:02
 * @Version: 1.0
 */
public class DeviceManage2 {
    //存储device对象至.txt格式文件中的方法
    public void saveDevice(Device device) {
        //对象信息
        String deviceid = Long.toBinaryString(device.deviceID);
        String bindStatus = Integer.toBinaryString(device.bindStatus);
        String workType = toBinary(Short.toString(device.workType));
        String s1_name = toBinary(device.s1.switchName);
        String s1_num = Integer.toBinaryString(device.s1.switchNumber);
        //创建文件来保存device对象
        File f = new File("");
        String path = f.getAbsolutePath() + "\\src\\富婆梦\\第一阶段\\第八周Scoket\\作业1\\device.txt";
        File file = new File(path);
        try {
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(deviceid);
            bufferedWriter.newLine();
            bufferedWriter.write(bindStatus);
            bufferedWriter.newLine();
/*            bufferedWriter.write(workType);
            bufferedWriter.newLine();*/
            bufferedWriter.write(s1_num);
            bufferedWriter.newLine();
            bufferedWriter.write(s1_name);
            bufferedWriter.flush();
            System.out.println("保存成功！");
            bufferedWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从properties文件中读取device对象的方法
    public Device loadDevice(String deviceId) {
        File f = new File("");
        String path = f.getAbsolutePath() + "\\src\\富婆梦\\第一阶段\\第八周Scoket\\作业1\\device.txt";
        File file = new File(path);

        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String id = bufferedReader.readLine();

            System.out.println(Long.parseLong(id,2));
            long deviceid = Long.parseLong(id,2);
            Integer bindStatus = Integer.parseInt(bufferedReader.readLine(),2);
            Byte.parseByte(bufferedReader.readLine(),2);
            Integer s1_num = Integer.parseInt(bufferedReader.readLine(),2);
            Switch s1 = new Switch();
            Switch s2 = new Switch();
            Switch s3 = new Switch();
            Switch s4 = new Switch();
            Short workType = 1;
            Device device = new Device(deviceid, bindStatus, workType, s1, s2, s3, s4);
            System.out.println("读取成功！");
            return device;
/*            if (Long.parseLong(deviceId,2) == Long.parseLong(id,2)) {
                Integer bindStatus = Integer.parseInt(bufferedReader.readLine(),2);
                Integer s1_num = Integer.parseInt(bufferedReader.readLine(),2);
                Switch s1 = new Switch();
                Switch s2 = new Switch();
                Switch s3 = new Switch();
                Switch s4 = new Switch();
                Short workType = 1;
                Device device = new Device(Long.parseLong(id), bindStatus, workType, s1, s2, s3, s4);
                System.out.println("读取成功！");
                return device;
            }*/


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //将字符串转成二进制码
    public String toBinary(String s) {
        char[] strChar = s.toCharArray();
        String result = "";
        for (int i = 0; i < strChar.length; i++) {
            result += Integer.toBinaryString(strChar[i]) + " ";
        }
        return result;
    }


    public static void main(String[] args) {
        DeviceManage2 deviceManage = new DeviceManage2();
        Device device = new Device();
        deviceManage.saveDevice(device);
        Device getDevice = deviceManage.loadDevice("20000001");
        System.out.println("deviceID:" + getDevice.deviceID);
        System.out.println("bindStatus:" + getDevice.bindStatus);
        System.out.println("workType:" + getDevice.workType);
        System.out.println("s1_name:" + getDevice.s1.switchName);
        System.out.println("s2_name:" + getDevice.s2.switchName);
        System.out.println("s3_name:" + getDevice.s3.switchName);
        System.out.println("s4_name:" + getDevice.s4.switchName);

    }
}
